package m1.client;

import java.util.Observable;
import java.util.Observer;

import m2.component.Component;
import m2.port.Port;
import tools.Message;

public class Client extends Component implements Observer{
	
	private static Client client = null;
	private ReceiveRequest receiveRequest;
	
	private Client(){
		addProperty(new PerfData(), new CodeSource());
		addComponent(new HighPerf(), new SmallPerf());		
		ClientInterfaceProvided clientInterfaceProvided = new ClientInterfaceProvided();
		ClientInterfaceRequired clientInterfaceRequired = new ClientInterfaceRequired();
		addComponentInterfaceProvided(clientInterfaceProvided);
		addComponentInterfaceRequired(clientInterfaceRequired);
		receiveRequest = new ReceiveRequest("ReceiveRequest");
		receiveRequest.observBy(this);
		addRequiredPort(receiveRequest);		
		addProvidedPort(new SendRequest("SendRequest"));
		clientInterfaceRequired.buildService();
		clientInterfaceProvided.buildService();		
	}	
	
	/**
	 * 
	 * @return
	 */
	public static Client getInstance(){
		if (client == null){
			client = new Client();
		}
		return client;
	}
	
	/**
	 * 
	 */
	public void connection(){

		startService("sendRequestService", "connect_ClientA");
		Message.putInLogger("I) Connection du client\n" +
				"- Port : SendRequest\n" +
				"- Value : connect_ClientA\n" );
	}

	/**
	 * 
	 * @param id
	 */
	public void getData(String id){

		startService("sendRequestService", "connect_" + id + "_ClientA");
		Message.putInLogger("II) SELECT * " +
						" FROM database" +
						" WHERE id = " + id);
	}
	
	@Override
	/**
	 * @param arg0
	 * @param arg1
	 */
	public void update(Observable observable, Object arg1) {
		Port observingPort = (Port) observable;
		/*if (observingPort == receiveRequest){*/
			Message.putInLogger(observingPort.getPortValue());			
		//}		
	}
	
}
